// 导入 defineStore  用于定义数据仓库
import { defineStore } from "pinia";

export const mainStore = defineStore("main", {
  state: () => {
    return {
      count: 10,
      list: [
        { id: "001", name: "OPPO", price: 200 },
        { id: "002", name: "XIAOMI", price: 300 },
        { id: "003", name: "VIVO", price: 400 },
      ],
    };
  },
  // 也可以定义为
  // state: () => ({ count: 0 })
  actions: {
    increment() {
      this.count++;
    },
  },
  getters: {
    sumPrice: (state)=> {
      return state.list.reduce( (pre, item) => {
        return pre + item.price
      }, 0);
    }
  }
});
